<template>

  <section>

    <div class="box">

      <div class="square" style="--i:0;"></div>
      <div class="square" style="--i:1;"></div>
      <div class="square" style="--i:2;"></div>
      <div class="square" style="--i:3;"></div>
      <div class="square" style="--i:4;"></div>
      <div class="square" style="--i:5;"></div>

      <div class="container" style="width: 360px;height: 430px">
        <div class="form">
          <h2>壮锦纹样在线设计平台</h2>

        <!--    登录表单      -->
          <form v-if="isLogin" action="">
            <div class="inputBx">
              <input title="请填写账号" type="text" required="required" v-model="username" style="width: 260px">
              <span><text style="font-family: 'FZYaoti'">账号</text></span>
              <i class="fas fa-user-circle"></i>
            </div>
            <div class="inputBx password">
              <input title="请填写密码" style="width: 260px" id="password-input" :type="isPasswordVisible ? 'text' : 'password'" v-model="password" required="required">
              <span><text style="font-family: 'FZYaoti'">密码</text></span>
              <a href="#" class="password-control" @click="togglePasswordVisibility"></a>
              <i class="fas fa-key"></i>
            </div>

            <div class="inputBx">
              <input type="submit" value="登录" style="font-weight: bolder" @click="login">
            </div>
          </form>

        <!--    注册表单      -->

          <form v-if="isReg" action="">
            <div class="inputBx">
              <input title="请填写账号" style="width: 260px" v-model="userAccount" type="text" required="required">
              <span><text style="font-family: 'FZYaoti'">请输入账号</text></span>
              <i class="fas fa-user-circle"></i>
            </div>
            <div class="inputBx password">
              <input title="请填写密码" style="width: 260px" id="password-input" :type="isPasswordVisible ? 'text' : 'password'" v-model="pass" required="required">
              <span><text style="font-family: 'FZYaoti'">请输入密码</text></span>
              <a href="#" class="password-control" @click="togglePasswordVisibility"></a>
              <i class="fas fa-key"></i>
            </div>

            <div class="inputBx password">
              <input title="请确认密码" style="width: 260px" id="password-input" :type="isPasswordVisibleConf ? 'text' : 'password'" v-model="passConf" required="required">
              <span><text style="font-family: 'FZYaoti'">请确认密码</text></span>
              <a href="#" class="password-control" @click="togglePasswordVisibilityConf"></a>
              <i class="fas fa-key"></i>
            </div>

            <div class="radio-group">
              <label class="radio-option">
                <input type="radio" value="client" v-model="userType" />
                <text class="userTypeText">来访者</text>
              </label>
              <label class="radio-option">
                <input type="radio" value="counselor" v-model="userType" />
                <text class="userTypeText">咨询师</text>
              </label>
            </div>


            <div class="inputBx" style="margin-top: 20px;display: flex;flex-direction: row">
              <input  @click="register" type="submit" value="注册" style="font-weight: bolder">
              <p><a href="#" @click="isRegButton=true;isLogin=true;isReg=false"><text style="font-family: 'STKaiti';margin-left: 30px;">返回登录</text></a></p>
            </div>




          </form>

          <p v-if="isRegButton" ><text style="font-weight: bolder">创建新账户&nbsp;:&nbsp;</text> <a href="#" @click="loginOrReg"><text style="font-family: 'STKaiti'">点击注册</text></a></p>
        </div>
      </div>

    </div>
  </section>

</template>

<script lang="ts" setup>
import { userLoginService,userRegisterService,getUserService} from "@/api/user.js"
import {onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
import {useUserStore} from "@/stores/user/user.js";
import Router from "@/router/index.js";
import { useRouter } from 'vue-router'
//首先在setup中定义
const router = useRouter()
const userStore=useUserStore()
const userType = ref(''); // 初始值为空字符串

onMounted( () => {
  //查询当前用户信息，如果有信息直接进行登录
  try {
    getUser()
  }catch (error){
    console.log(error)
  }

});


const getUser=(async ()=>{
  const res=await getUserService()
  if (res!=undefined&&res.code===200){
    //存储用户信息
    userStore.setUser(res.data)
    //跳转到导航页
    if (res.data!==null){
      await router.push('/layout')
    }
  }
})


//控制密码显隐
const isPasswordVisible=ref(false)
const togglePasswordVisibility=(()=>{
  isPasswordVisible.value=!isPasswordVisible.value
})

const isPasswordVisibleConf=ref(false)
const togglePasswordVisibilityConf=(()=>{
  isPasswordVisibleConf.value=!isPasswordVisibleConf.value
})




//控制注册还是登录
const isLogin=ref(true)
const isReg=ref(false)

const isRegButton=ref(true)
const loginOrReg=(()=>{
  isLogin.value=false
  isReg.value=true
  isRegButton.value=false
})


const userAccount=ref('')
const pass=ref('')
const passConf=ref('')
const registerData=ref({})
//注册事件
const register=(async (event) => {
  event.preventDefault()
  if (userAccount.value == '' || pass.value == '' || passConf == '') {
    ElMessage.warning("请填写完整")
  } else if (pass!.value !== passConf.value) {
    ElMessage.warning("两次密码不相同")
  } else if (userType.value == '') {
    ElMessage.warning("请选择角色")
  } else {
    registerData.value = {
      username: userAccount.value,
      password: pass.value,
      usertype: userType.value,
      status:'0'
    }

    const res=await userRegisterService(registerData.value);
    if (res.code===200){
      ElMessage.success("注册成功")
      isLogin.value = true
      isReg.value = false
      isRegButton.value = true
    }
  }



})


//登录事件
const username=ref('lisi')
const password=ref('12345')
const loginData=ref({})
const loginToken=ref('')
const login=(async (event)=>{
  event.preventDefault()
  if (username.value!=''&&password.value!=''){
    loginData.value={
      username:username.value,
      password:password.value
    }
    const res=await userLoginService(loginData.value)
    console.log(res)
    if (res.code===200){
      //存储token和用户信息
      ElMessage.success(res.data)
      loginToken.value=res.message
      userStore.setToken(loginToken.value)
      await router.push('/layout')
    }else {
      ElMessage.warning(res.errMsg)
    }



  }else {
    ElMessage.warning("请先输入账号密码")
  }
})

</script>

<style>

.userTypeText{
  font-family: 'FZYaoti'
}

.radio-group {
  display: flex;
  gap: 20px; /* 控制选项之间的间距 */
}

.radio-option {
  display: flex;
  align-items: center;

  background-color: transparent; /* 透明背景 */
  cursor: pointer;
}

.radio-option input {
  margin-right: 8px; /* 选项与文本之间的间距 */
}


@import url('https://fonts.googleapis.com/css2?family=El+Messiri:wght@700&display=swap');

* {
  margin: 0;
  padding: 0;
  font-family: 'El Messiri', sans-serif;
}

body {
  //background: #031323;
  overflow: hidden;
}

.fas {
  width: 32px;
}

section {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
  background-size: 400% 400%;
  animation: gradient 10s ease infinite;
}

@keyframes gradient {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.box {
  position: relative;

  .square {
    position: absolute;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(5px);
    box-shadow: 0 25px 45px rgba(0, 0, 0, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.15);
    border-radius: 15px;
    animation: square 10s linear infinite;
    animation-delay: calc(-1s * var(--i));
  }

  @keyframes square {
    0%,100% {
      transform: translateY(-20px);
    }

    50% {
      transform: translateY(20px);
    }
  }

  .square:nth-child(1) {
    width: 100px;
    height: 100px;
    top: -15px;
    right: -45px;
  }

  .square:nth-child(2) {
    width: 150px;
    height: 150px;
    top: 105px;
    left: -125px;
    z-index: 2;
  }

  .square:nth-child(3) {
    width: 60px;
    height: 60px;
    bottom: 85px;
    right: -45px;
    z-index: 2;
  }

  .square:nth-child(4) {
    width: 50px;
    height: 50px;
    bottom: 35px;
    left: -95px;
  }

  .square:nth-child(5) {
    width: 50px;
    height: 50px;
    top: -15px;
    left: -25px;
  }

  .square:nth-child(6) {
    width: 85px;
    height: 85px;
    top: 165px;
    right: -155px;
    z-index: 2;
  }
}

.container {
  position: relative;
  padding: 50px;
  width: 260px;
  min-height: 380px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
  border-radius: 10px;
  box-shadow: 0 25px 45px rgba(0, 0, 0, 0.2);
}

.container::after {
  content: '';
  position: absolute;
  top: 5px;
  right: 5px;
  bottom: 5px;
  left: 5px;
  border-radius: 5px;
  pointer-events: none;
  background: linear-gradient( to bottom, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0.1) 2%
  );
}

.form {
  position: relative;
  width: 100%;
  height: 100%;

  h2 {
    color: #fff;
    letter-spacing: 2px;
    margin-bottom: 30px;
  }

  .inputBx {
    position: relative;
    width: 100%;
    margin-bottom: 20px;

    input {
      width: 80%;
      outline: none;
      border: none;
      border: 1px solid rgba(255, 255, 255, 0.2);
      background: rgba(255, 255, 255, 0.2);
      padding: 8px 10px;
      padding-left: 40px;
      border-radius: 15px;
      color: #fff;
      font-size: 16px;
      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
    }

    .password-control {
      position: absolute;
      top: 11px;
      right: 10px;
      display: inline-block;
      width: 20px;
      height: 20px;
      background: url(https://snipp.ru/demo/495/view.svg) 0 0 no-repeat;
      transition: 0.5s;
    }

    .view {
      background: url(https://snipp.ru/demo/495/no-view.svg) 0 0 no-repeat;
      transition: 0.5s;
    }



    .fas {
      position: absolute;
      top: 13px;
      left: 13px;
    }

    input[type="submit"] {
      background: #fff;
      color: #111;
      max-width: 100px;
      padding: 8px 10px;
      box-shadow: none;
      letter-spacing: 1px;
      cursor: pointer;
      transition: 1.5s;
    }

    input[type="submit"]:hover {
      background: linear-gradient(115deg,
      rgba(0,0,0,0.10),
      rgba(255,255,255,0.25));
      color: #fff;
      transition: .5s;
    }

    input::placeholder {
      color: #fff;
    }

    span {
      position: absolute;
      left: 30px;
      padding: 10px;
      display: inline-block;
      color: #fff;
      transition: .5s;
      pointer-events: none;
    }

    input:focus ~ span,
    input:valid ~ span {
      transform: translateX(-30px) translateY(-25px);
      font-size: 12px;
    }
  }

  p {
    color: #fff;
    font-size: 15px;
    margin-top: 5px;

    a {
      color: #fff;
    }

    a:hover {
      background-color: #000;
      background-image: linear-gradient(to right, #434343 0%, black 100%);
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
    }
  }
}

.remember {
  position: relative;
  display: inline-block;
  color: #fff;
  margin-bottom: 10px;
  cursor: pointer;
}
</style>